package com.cch.android.activity.view;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;

import com.cch.android.activity.ModelLandscapActivity;
import com.cch.android.activity.ModelLandscapActivity.SeekBarDialog;
import com.cch.android.activity.R;
import com.cch.android.imageutil.MixColor;
import com.cch.android.util.Constant;


public class SetColorActivity extends Activity implements OnSeekBarChangeListener,OnClickListener{

	    private LinearLayout tone_view_lay;
	    /**
	     * 调色工具类
	     */
	    private MixColor mMixColor;
	    
	    
	    private ImageView mImageView;
	    /**
	     * 调色图片
	     */
	    private Bitmap mBitmap; 
	    
	    /**
		 * 变化后的图片
		 */
		private Bitmap bmpSet;
		/**
		 * 本地图片路径集合
		 */
		private String path;
		
		private Button effect_close,effect_save;
	   @Override
	   public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.model_setimage_setcolor);
		Intent intent = getIntent();
		mBitmap = intent.getParcelableExtra("imageBmp");
		initView();
	}
	
	
	    public void initView(){
	        tone_view_lay = ((LinearLayout) findViewById(R.id.tone_view));
	        mImageView = ((ImageView) findViewById(R.id.setimage1_setcolor_image));
	        effect_close = (Button)findViewById(R.id.setimage1_setcolor_colse);
	        effect_save = (Button)findViewById(R.id.setimage1_setcolor_save);
	        effect_close.setOnClickListener(this);
	        effect_save.setOnClickListener(this);
	        if(mBitmap!=null)
	        mImageView.setImageBitmap(mBitmap);
	        mMixColor = new MixColor(this);  
	        tone_view_lay.addView(mMixColor.getParentView()); 
	        ArrayList<SeekBar> seekBars = mMixColor.getSeekBars();   
	        for (int i = 0, size = seekBars.size(); i < size; i++)  
	        {  
	            seekBars.get(i).setOnSeekBarChangeListener(this);  
	        }  
	        
	    }
	    
		@Override  
	    public void onProgressChanged(SeekBar seekBar, int progress,  
	            boolean fromUser) {  
	        int flag = (Integer) seekBar.getTag();  
	        switch (flag)  
	        {  
	        case MixColor.FLAG_SATURATION:  
	        	mMixColor.setSaturation(progress);  
	        	SeekBarDialog.FLAG_SATURATION_PROGRESS= progress;
	            break;  
	        case MixColor.FLAG_LUM:  
	        	mMixColor.setLum(progress); 
	        	SeekBarDialog.FLAG_LUM_PROGRESS =progress;
	            break;  
	        case MixColor.FLAG_HUE:  
	        	mMixColor.setHue(progress); 
	        	SeekBarDialog.FLAG_HUE_PROGRESS = progress;
	            break;  
	        }  
	        mImageView.setImageBitmap(mMixColor.handleImage(mBitmap, flag));  
	    }  
	  
	    @Override  
	    public void onStartTrackingTouch(SeekBar seekBar) {  
	          
	    }  
	  
	    @Override  
	    public void onStopTrackingTouch(SeekBar seekBar) {  
	          
	    }  
	    
		@Override
		protected void onDestroy() {
			if (mBitmap != null) {
				mBitmap.recycle();
				System.gc();
				mBitmap=null;
			}
			if (bmpSet != null) {
				bmpSet.recycle();
				System.gc();
				bmpSet=null;
			}
			super.onDestroy();
		}


		private void backToMain(){
			Intent intent = new Intent();
			intent.putExtra("imageBmp", mBitmap);
			intent.setClass(this, ModelLandscapActivity.class);
			startActivity(intent);
		}
		/**
		 * 保存到sd卡 回到美化图片主界面
		 */
		private void saveToMain(){
			/*Drawable drawable = mImageView.getDrawable();
			if(drawable!=null)
				try {
					Constant.saveMyBitmap("setImage", 100, drawable);
				} catch (IOException e) {
					e.printStackTrace();
				}*/
			Intent intent = new Intent();
			intent.putExtra("imageBmp", getSetBitmap());
			intent.setClass(this, ModelLandscapActivity.class);
			startActivity(intent);
		}
		/**
		 * 得到处理的图片
		 * @return
		 */
		private Bitmap getSetBitmap(){
			if (mImageView != null) {
				if (bmpSet != null) {
					bmpSet.recycle();
					System.gc();
					bmpSet=null;
				}
				bmpSet = Constant.drawable2Bitmap(mImageView.getDrawable());
				return bmpSet;
			}
			return bmpSet;
		}


		@Override
		public void onClick(View v) {
			switch (v.getId()) {
			case R.id.setimage1_setcolor_colse:
//				backToMain();
				finish();
				break;
			case R.id.setimage1_setcolor_save:
				saveToMain();
				break;
			}
			
		}

}
